(function($) {
	var datagrid = '#dg_oauthUser', toolbar = '#toolbars_oauthUser', del = '#delete_oauthUser', reload = '#reload_oauthUser',
		form = '#form_oauthUser', submit = '#submit', closeWin = '#closeWin', search = '#search_oathuser';
	
	var oauthUserObj = function() {
		var self = this ;
		this.dg = null ;
		this.url_form = window.app.domain.URL_ADMIN_UI + '/pages/weixin/oauthuser/form.html' ;
		this.url_list = window.app.domain.URL_API_WEIXIN_REST + '/weixin/oauthUser/list' ;
		this.url_add = window.app.domain.URL_API_WEIXIN_REST + '/weixin/oauthUser/add' ;
		this.url_update = window.app.domain.URL_API_WEIXIN_REST + '/weixin/oauthUser/update' ;
		this.url_delete = window.app.domain.URL_API_WEIXIN_REST + '/weixin/oauthUser/delete' ;
		this.url_get = window.app.domain.URL_API_WEIXIN_REST + '/weixin/oauthUser/get' ;
		
		this.initDatagrid() ;
		this.initBindBtnEvent();
		
		//搜索
		$(search).searchbox({
			menu:'#mm_oathuser',
			searcher:function(value,name){
				var params = {} ;
				params[name] = value ;
				self.dg.datagrid('load', params);
		    }
		});
	};
	
	oauthUserObj.prototype.initDatagrid = function() {
		var self = this ;
		this.dg = $(datagrid).datagrid({
			url: self.url_list, 
			method: 'GET', nowrap: true, striped: true, singleSelect: false,
			checkbox: true, pagination: true, pageSize: 30, pageList: [30,40,50,100,200,500,1000],
			autoRowHeight: true, fit: true, border: false, rownumbers: true, rownumberWidth: 40, toolbar: toolbar, 
			remoteSort: true, sortName: 'id', sortOrder: 'desc', queryParams: {},
			frozenColumns: [[
			           {title: 'ID', field: 'id', hidden: true},
			           {field: 'ck', checkbox:true}
			]],
			columns: [[
			           {title: '用户头像', field: 'headimgurl', align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   var str = [
			        	              '<div style="width: 50px; height: 50px;margin: 5px;">',
			        	              '<img style="width: 100%;height: 100%;" src="'+value.substring(0, value.lastIndexOf("/"))+'/46">',
			        	              '</div>'
			        	             ];
			        	   return str.join('');
			           }},
			           {title: 'openid', field: 'openid', width: 200},
			           {title: '昵称', field: 'nickname', width: 100},
			           {title: '性别', field: 'sex', width: 60, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value==1?'男':'女';
			           }},
			           {title: '手机号码', field: 'mobile', width: 110},
			           {title: '国家', field: 'country', width: 80},
			           {title: '省份', field: 'province', width: 80},
			           {title: '城市', field: 'city', width: 80},
			           {title: '特权信息', field: 'privilege', width: 130},
			           {title: '开放平台绑定', field: 'unionid', width: 130},
			           {title: '最后访问时间', field: 'lastAccessTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			           {title: '最后访问IP', field: 'lastAccessIp', width: 130},
			           {title: '创建时间', field: 'createTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
			        	   return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
			           }},
			]],
			onLoadSuccess: function(data) {
			},
			onLoadError: function() {
			}
		});
	};
	
	oauthUserObj.prototype.initBindBtnEvent = function() {
		var self = this ;
		$(del).on('click', function() { self.del() });
		$(reload).on('click', function() { self.reload() });
	};
	
	oauthUserObj.prototype.del = function() {
		var self = this ;
		var selections = window.app.easyui.getSelections(datagrid) ;
		if(selections) {
			$.messager.confirm('删除确认', '确定要删除？', function(r) {
				if(r) {
					$.ajax({
						type: 'GET',
						url: self.url_delete + '/' + selections.join(','),
						beforeSend: function() {
							$.easyui.loading();
						},
						success: function(result) {
							try {
								if(result.status) {
									self.reload(); alertify.success(result.message);
								} else {
									alertify.warning(result.message);
								}
							} catch(e) {
								$.easyui.loaded(); console.error(e) ;
							}
						},
						complete: function(xhr, textStatus) {
							$.easyui.loaded();
						}
					});
				}
			});
		} else {
			alertify.error('至少选择一条记录');
		}
	};
	
	oauthUserObj.prototype.get = function(id) {
		var self = this ;
		$.ajax({
			type: 'GET',
			url: self.url_get + id,
			beforeSend: function() {
				$.easyui.loading();
			},
			success: function(result) {
				try {
					return result ;
				} catch(e) {
					$.easyui.loaded(); console.error(e) ;
				}
			},
			complete: function(xhr, textStatus) {
				$.easyui.loaded();
			}
		});
	};
	
	oauthUserObj.prototype.submitForm = function(url) {
		var self = this ;
		var checkform = $(form).form('validate');
		if(checkform) {
			$.ajax({
				type: 'POST',
				url: url,
				data: window.app.util.getData(form),
				beforeSend: function() {
					$.easyui.loading();
				},
				success: function(result) {
					try {
						if(result.status) {
							self.reload(); window.app.easyui.closeCurrentWindow(); alertify.success(result.message);
						} else {
							alertify.warning(result.message);
						}
					} catch(e) {
						$.easyui.loaded(); console.error(e) ;
					}
				},
				complete: function(xhr, textStatus) {
					$.easyui.loaded();
				}
			});
		}
	};
	
	oauthUserObj.prototype.reload = function() {
		this.dg.datagrid('reload'); this.dg.datagrid('clearSelections');this.dg.datagrid('clearChecked');
	};
	
	var oauthUser = new oauthUserObj() ;
})(jQuery);
	
